
from google.appengine.ext.db import Key

from dbmodels.weapon import WeaponModel
from trait import TraitMessage

from protorpc import messages
from protorpc import message_types
from protorpc import remote

package = "api"

class WeaponMessage(messages.Message):
    id = messages.StringField(1, required=True)
    name = messages.StringField(2, required=True)
    label = messages.StringField(3, required=False)
    range = messages.IntegerField(4, required=True)
    traits = messages.MessageField(TraitMessage, 5, repeated=True)

class WeaponService(remote.Service):

    @remote.method(WeaponMessage, message_types.VoidMessage)
    def add(self, request):
        weapon = WeaponModel(
            key_name = request.id,
            name = request.name,
            label = request.label,
            range = request.range
        )

        for trait in request.traits:
            trait_k= Key.from_path('TraitModel', trait.id)
            weapon.traits.append(trait_k)

        weapon.put()

        return message_types.VoidMessage()
